﻿using System;
using Common.Logging;
using SimpleServiceBus.Bus.MessageManagement.MessageHandling;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Messages.Subscriptions;

namespace SimpleServiceBus.Bus.Subscriptions
{
    public class SubscriptionRequestHandler : IMessageHandler<SubscribeToAllRequest>,
                                              IMessageHandler<SubscribeToMessageRequest>,
                                              IMessageHandler<UnsubscribeFromAllRequest>,
                                              IMessageHandler<UnsubscribeFromMessageRequest>
    {

        public void Handle(SubscribeToAllRequest message, IMessageContext context)
        {
            if (String.IsNullOrEmpty(message.SubscriberAddress))
                throw new InvalidOperationException("An subscription request without a subscriber address was received.");

            var mgr = context.GetLocalService<ISubscriptionManager>();
            if (mgr != null)
            {
                mgr.SubscribeToAll(message.SubscriberAddress);
            }
            else
            {
                throw new InvalidOperationException(
                    "A subscription request was processed, but there is no subscription manager configured to service the request.");
            }
        }

        public void Handle(SubscribeToMessageRequest message, IMessageContext context)
        {
            if (String.IsNullOrEmpty(message.SubscriberAddress))
                throw new InvalidOperationException("An subscription request without a subscriber address was received.");

            var mgr = context.GetLocalService<ISubscriptionManager>();
            if (mgr != null)
            {
                Type messageType = Type.GetType(message.MessageType);

                mgr.SubscribeToMessage(messageType,
                                       message.SubscriberAddress);
            }
            else
            {
                throw new InvalidOperationException(
                    "A subscription request was processed, but there is no subscription manager configured to service the request.");
            }
        }

        public void Handle(UnsubscribeFromAllRequest message, IMessageContext context)
        {
            if (String.IsNullOrEmpty(message.SubscriberAddress))
                return;

            var mgr = context.GetLocalService<ISubscriptionManager>();
            if (mgr != null)
            {
                mgr.UnsubscribeFromAll(message.SubscriberAddress);
            }
        }

        public void Handle(UnsubscribeFromMessageRequest message, IMessageContext context)
        {
            if (String.IsNullOrEmpty(message.SubscriberAddress))
                return;

            var mgr = context.GetLocalService<ISubscriptionManager>();
            if (mgr != null)
            {
                Type messageType = Type.GetType(message.MessageType);

                mgr.UnsubscribeFromMessage(messageType,
                                           message.SubscriberAddress);
            }
        }
    }
}